package com.nacos.nacosconsumer;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author wenl
 * @version V2.0
 * @project 自己的框架
 * @package com.nacos.nacosconsumer
 * @date 2020/4/2
 * @describe
 */

@RestController
@RefreshScope
public class ConsumerController {

    @Autowired
    private EchoRemoter echoRemoter;

    @Value("${user.name}")
    String userName;

    @Value("${user.age}")
    int age;

    @NacosInjected
    private NamingService namingService;

    @Autowired
    @LoadBalanced
    private RestTemplate restTemplate;

    @GetMapping("echo")
    public String echo(){

//        System.out.println(restTemplate.getForObject("http://nacos-producer/echo/"+userName+"-"+age, String.class)+"------------");
        return echoRemoter.echo(userName);

    }

    @GetMapping("hi")
    public String hi(){

        return echoRemoter.echo(userName);

    }

    @GetMapping("echo-nacos")
    public List<Instance> echoNacos(@RequestParam String serviceName) throws NacosException {

        List<Instance> result = namingService.getAllInstances(serviceName);
        result.forEach(System.out::println);
        return result;
    }



}
